import axios from "axios";
import { message as toast } from "antd";
import { isArray } from "lodash";
import { baseApi } from '../../public/config.json'

const instance = axios.create({
  baseURL: baseApi,
});

enum ErrorCode {
  内部错误 = 10000,
  参数错误 = 10001,
  库ID不能存在 = 10002,
  库名重复 = 10003,
  库数量超过上限 = 10004,
  无效的库大小 = 10005,
  超过任务限制数量 = 10010,
  至少选择一个解析模块 = 10011,
  解析模块不支持 = 10012,
  文件不存在 = 10020,
}

instance.interceptors.request.use();
instance.interceptors.response.use(({ data, config }) => {
  const { code, message, msg, data: result } = data;
  if (code === "0000") {
    if (config.isCustomData) {
      return result;
    }
    if (isArray(result?.list)) {
      return result.list;
    }
    return result || data;
  }

  if (code === "特征提取接口异常") {
    toast.error("特征提取接口异常").then();
  }

  if (msg) {
    toast.error(msg).then();
  }

  if (code === "图片内容不符合单元素要求") {
    toast.error('图片内容不符合单元素要求"').then();
  }
  if (message) {
    toast.error(ErrorCode[code] || message).then();
  }
  return Promise.reject(data);
});

export default instance;
